#include <vector>

using std::vector;

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        if (nums.size() == 0) {
            return 0;
        }
        int i = 0, j = nums.size() - 1;
        while (i < j) {
            while (nums[i] != val && i < j) {
                i++;
            }
            while (nums[j] == val && i < j) {
                j--;
            }
            if (i != j) {
                std::swap(nums[i], nums[j]);
            }
        }
        return nums[i] == val ? i : i + 1;
    }
};